# -*- coding: utf-8 -*-



## ------------------------------------------------------------
## Copyright 2010 Pierre-Antoine Delsart
## 
## This file is part of LeVin.
## 
## LeVin is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## LeVin is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with LeVin.  If not, see <http://www.gnu.org/licenses/>.
## ---------------------------------------------------------------

from PythonQt import QtCore, QtGui,QtSvg
import sys
import os.path
import wineBase
_join = os.path.join

qttranslate = QtGui.QApplication.translate
qtUtf8 = QtGui.QApplication.UnicodeUTF8
APPNAME = "LeVin"

ICONDIR=''
def getIcon(name):
    return _join(ICONDIR , name)

DATADIR=''
def getData(name):
    return _join(DATADIR , name)


USERCONFDIR = None
app = QtGui.QApplication(sys.argv)

## Translation process --------------------------------------
locale = QtCore.QLocale.system().name()
qtTranslator = QtCore.QTranslator()
appTranslator = QtCore.QTranslator()
qttranslateU = appTranslator.trUtf8
qttranslate0 = appTranslator.tr

def setupTranslation():
    print ' Setting up Translation ',locale
    if qtTranslator.load("qt_" + locale):
        app.installTranslator(qtTranslator)

    if appTranslator.load(getData("levin_" + str(locale))):
        print 'Translating to ',locale
        app.installTranslator(appTranslator)
def translate(text, comment=None,coding=qtUtf8):
    return qttranslateU(text)
def trUtf8(text, comment=None,coding=qtUtf8):
    return qttranslate("@default",text.decode("utf-8"),comment,coding)
    #return qttranslate0(text)
def tr(text, comment=None,coding=qtUtf8):
    return qttranslate("@default",text,comment,coding)

# -------------------------------------------------------------

# Main window -----------------------------------------
window = None
def setupMainWindow():
    global window
    window = QtGui.QMainWindow()
    window.setObjectName(APPNAME)
# -------------------------------------------------------------    



class FixedWidthSVG(QtSvg.QSvgWidget ):
    ratio = 1
    def __init__(self,parent):
        QtSvg.QSvgWidget.__init__(self, parent)
        p = QtGui.QSizePolicy.Preferred
        sizePolicy = QtGui.QSizePolicy(p,p)
        sizePolicy.setHeightForWidth(True)
        self.setSizePolicy(sizePolicy)
    def load(self, path):
        QtSvg.QSvgWidget.load(self,path)
        s=self.renderer().defaultSize()
        self.ratio = float( s.height() ) / float( s.width() )

    def heightForWidth(self, width):
        return int(width*self.ratio)
# -------------------------------------------------------------    


connect = QtCore.QObject.connect

class WinePixmaps(object):

    noratePix_small = None
    pixRates_small = None    

    @classmethod
    def setup(cls):
        if cls.noratePix_small is not None:
            return        
        cls.noratePix_small = QtGui.QPixmap(getIcon("noratePic.svg"))
        cls.pixRates_small  = [ QtGui.QPixmap(getIcon("ratingGlass%d.svg")%(i,)) for i in range(11)]


_qtopenurl = QtGui.QDesktopServices.openUrl
_qurl = QtCore.QUrl
def openUrl( url ):
    _qtopenurl( _qurl( url ) )


googleBaseStr = 'http://www.google.com/'

def webSearchUrl_google(a,c):
    return googleBaseStr+'search?q="%s"+"%s"'%(a.replace(' ','+'), c.replace(' ','+') )

webSearchUrl = webSearchUrl_google



